/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.excel.read;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.star.framework.excel.core.Column;
import cn.star.framework.excel.read.status.RowStatus;
import cn.star.framework.excel.read.status.SheetStatus;
import cn.star.framework.excel.read.status.ReadStatus;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * 读取Excel时的上下文信息<br>
 * 生命周期未读取一个完整的Excel文件<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Slf4j
public class ReadContext {

  /** 数据结构 */
  protected List<Column> columns;

  /** 每一个Sheet读取的状态 */
  @Getter protected List<SheetStatus> sheetStatuses = new ArrayList<>();

  /** 判处不需要导入的字段 */
  private final Set<String> excludeColumns = new HashSet<>();

  /**
   * Sheet数量<br>
   * 默认处理一个
   */
  @Getter @Setter protected int sheetCount = 1;

  /** 读取状态 */
  @Setter protected ReadStatus status = ReadStatus.NOT_STARTED;

  /** Excel原始文件 */
  @Getter @Setter protected File file;

  /** 读取一批数据调用{@link ReadRowCallback}函数 */
  @Getter @Setter protected int batchSize = 100;

  /** 记录读取数据过程中出现的异常 */
  @Getter @Setter protected Throwable throwable;

  public ReadStatus getStatus() {
    // 出现异常即为 UNFINISHED 状态
    return throwable != null ? ReadStatus.UNFINISHED : status;
  }

  protected ReadContext() {}

  public ReadContext(List<Column> columns) {
    this.columns = columns;
  }

  /**
   * 获取未被忽略的列
   *
   * @return
   */
  public List<Column> getIncludeColumns() {
    return columns.stream()
        .filter(i -> !i.isIgnore() && !excludeColumns.contains(i.getKey()))
        .collect(Collectors.toList());
  }

  /**
   * 外部读取处理结果
   *
   * @return
   */
  public List<SheetStatus> readSheetStatuses() {
    List<SheetStatus> list = new ArrayList<>();
    for (SheetStatus sheetStatus : this.sheetStatuses) {
      List<RowStatus> rowStatuses = sheetStatus.getExternalNotReadRowStatuses();
      if (CollectionUtil.isNotEmpty(rowStatuses)) {
        SheetStatus status = new SheetStatus(sheetStatus.getName());
        status.setStatus(sheetStatus.getStatus());
        status.setErrorType(sheetStatus.getErrorType());
        status.setMessage(sheetStatus.getMessage());
        for (RowStatus rowStatus : rowStatuses) {
          rowStatus.setExternalReadStatus(true);
          status.addRowStatus(rowStatus);
        }
        list.add(status);
      }
    }
    return !list.isEmpty() ? list : null;
  }

  /**
   * 判断处理结果是否已经被外部全部读取
   *
   * @return
   */
  public boolean isExternalReadSheetRowStatusesFinish() {
    for (SheetStatus sheetStatus : this.sheetStatuses) {
      List<RowStatus> rowStatuses = sheetStatus.getExternalNotReadRowStatuses();
      if (!rowStatuses.isEmpty()) {
        return false;
      }
    }

    return true;
  }

  public ReadContext addExcludeColumns(String... names) {
    if (ArrayUtil.isNotEmpty(names)) {
      excludeColumns.addAll(CollectionUtil.toList(names));
    }

    return this;
  }
}
